package com.wolfpire.workflow.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.workflow.dao.NodeDao;
import com.wolfpire.workflow.em.NodeType;
import com.wolfpire.workflow.om.Node;

@Repository("nodeDao")
public class NodeDaoImpl extends BaseHibernateDao<Node, Long> implements NodeDao {
	
	public Node getNode(Long deploymentId, String nodeName) {
		Criteria criteria = createCriteria(
				Restrictions.eq("deploymentId", deploymentId),
				Restrictions.eq("nodeName", nodeName));
		return (Node) criteria.uniqueResult();
	}
	
	/**
	 * 根据流程定义ID获取流程步骤列表
	 * @param deploymentId 流程定义ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Node> getNodeListByDeploymentId(Long deploymentId) {
		Criteria criteria = createCriteria(Restrictions.eq("deploymentId", deploymentId));
		return criteria.list();
	}
	
	
	@SuppressWarnings("unchecked")
	public Node getStartNode(Long deploymentId, NodeType nodeType) {
		Criteria criteria = createCriteria(
				Restrictions.eq("deploymentId", deploymentId),
				Restrictions.eq("nodeType", nodeType.getIndex()));
		List<Node> list = criteria.list();
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		
		return null;
	}

}
